package web.visitor;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController  
@RequestMapping("/visitor") 
public class VisitorClient {
	
	@RequestMapping("/{type}")  
    public Object view(@PathVariable("type") Integer type) {  
        Component root = new Composite("root");
        Component c1 = new Composite("c1");
        Component c2 = new Composite("c2");
        Component c1leaf1 = new Leaf("c1leaf1");
        Component c2leaf1 = new Leaf("c2leaf1");
        Component c1leaf2 = new Leaf("c1leaf2");
        Component c2leaf2 = new Leaf("c2leaf2");
        root.addChild(c1);
        root.addChild(c2);
        c1.addChild(c1leaf1);
        c1.addChild(c1leaf2);
        c2.addChild(c2leaf1);
        c2.addChild(c2leaf2); 
        //每种数据结构都有固定的访问者
        Visitor leaf = new Lvisit();
        Visitor ceaf = new Cvisit();
        root.accept(ceaf);
        c1leaf1.accept(leaf);
        return "成功!";  
    }  	
	public static void main(String[] args) {
		Component root = new Composite("root");
        Component c1 = new Composite("c1");
        Component c2 = new Composite("c2");
        Component c1leaf1 = new Leaf("c1leaf1");
        Component c2leaf1 = new Leaf("c2leaf1");
        Component c1leaf2 = new Leaf("c1leaf2");
        Component c2leaf2 = new Leaf("c2leaf2");
        root.addChild(c1);
        root.addChild(c2);
        c1.addChild(c1leaf1);
        c1.addChild(c1leaf2);
        c2.addChild(c2leaf1);
        c2.addChild(c2leaf2); 
        Visitor leaf = new Lvisit();
        Visitor ceaf = new Cvisit();
        root.accept(ceaf);
        c1leaf1.accept(leaf);
	}
}
